package com.weilive.platform.gate.json.handler;

import org.springframework.stereotype.Service;

import com.weilive.core.netty.NettyErrors;
import com.weilive.core.protocol.json.JsonRequest;
import com.weilive.core.protocol.json.JsonRequestHandler;
import com.weilive.core.protocol.json.JsonResponse;
import com.weilive.core.utils.MD5Utils;
import com.weilive.core.utils.RegexUtils;
import com.weilive.platform.PlatformConstants;
import com.weilive.platform.PlatformErrors;
import com.weilive.platform.PlatformSecurity;
import com.weilive.platform.cache.user.UserManager;
import com.weilive.platform.cache.user.UserObj;
import com.weilive.platform.gate.json.protocol.CSA_PlatUserPasswd;
import com.weilive.platform.gate.json.protocol.CSQ_PlatUserPasswd;

/**
 * 
 * 平台用户修改密码
 * 
 * @author houge
 *
 */
@Service
public class CSQ_PlatUserPasswd_JsonHandler extends JsonRequestHandler {

	public Class<?> getRequestType() {
		return CSQ_PlatUserPasswd.class;
	}

	public Class<?> getResponseType() {
		return CSA_PlatUserPasswd.class;
	}

	private boolean checkSign(CSQ_PlatUserPasswd params, String secret) throws Exception {
		long delta = System.currentTimeMillis() - params.date;
		if (delta > PlatformConstants.SERVER_SIGN_VALID_INTERVAL
				|| delta < -PlatformConstants.SERVER_SIGN_VALID_INTERVAL) {
			return false;
		}
		String sign = MD5Utils.encryptToHex(secret + params.date
				+ MD5Utils.encryptToHex(params.app_id + params.uin + params.token + params.old_pwd + params.new_pwd));
		return params.sign.equals(sign);
	}

	public JsonResponse handleRequest(JsonRequest request) throws Exception {
		CSQ_PlatUserPasswd params = (CSQ_PlatUserPasswd) request.getData();
		if (params.date == null || params.sign == null || params.app_id == null || params.uin == null
				|| params.token == null || params.new_pwd == null || params.old_pwd == null) {
			return createErrorResponse(NettyErrors.BAD_REQUEST);
		}

		// 获取用户
		Long user_id = UserManager.getUserId(params.uin, params.app_id, params.token);
		if (user_id == null) {
			return createErrorResponse(PlatformErrors.INVALID_TOKEN);
		}
		UserObj user = UserManager.getUser(user_id);
		if (user == null) {
			return createErrorResponse(PlatformErrors.USER_NOT_EXIST);
		}

		// 检测签名
		if (checkSign(params, user.info.secret) == false) {
			return createErrorResponse(PlatformErrors.USER_SIGN_FAILED);
		}

		// 读取老密码并验证
		String old_pwd = PlatformSecurity.base64ServerDecrypt(params.old_pwd);
		if (old_pwd == null || RegexUtils.isPassword(old_pwd) == false) {
			return createErrorResponse(PlatformErrors.INVALID_PASSWORD);
		}
		if (user != null && MD5Utils.encryptToHex(old_pwd).equals(user.info.password) == false) {
			return createErrorResponse(PlatformErrors.WRONG_PASSWORD);
		}

		// 读取新密码并设置
		String new_pwd = PlatformSecurity.base64ServerDecrypt(params.new_pwd);
		if (new_pwd == null || RegexUtils.isPassword(new_pwd) == false) {
			return createErrorResponse(PlatformErrors.INVALID_PASSWORD);
		}
		UserManager.setUserPassword(user_id, new_pwd);

		// 反馈
		CSA_PlatUserPasswd response = new CSA_PlatUserPasswd();
		return JsonResponse.create(response);
	}

}
